import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution199 {
    /**
     * 给定一棵二叉树，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
     */

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) return list;
        Queue<TreeNode> queue1 = new LinkedList<>();
        queue1.offer(root);
        Queue<TreeNode> queue2 = new LinkedList<>();
        int pre = 0;
        while (!queue1.isEmpty()) {
            while (!queue1.isEmpty()) {
                TreeNode node = queue1.poll();
                if (node.left != null) queue2.offer(node.left);
                if (node.right != null) queue2.offer(node.right);
                pre = node.val;
            }
            list.add(pre);
            Queue<TreeNode> temp = queue1;
            queue1 = queue2;
            queue2 = temp;
        }
        return list;
    }
}
